var $;
var element;
var layer;
layui.use(['element','layer'], function() {
    //定义调用模块
    $ = layui.$;
    element = layui.element;
    layer=layui.layer;
    /*加载垂直导航栏数据*/
    $.ajax({
        url : '/Navigation/selNavigation',
        type : "post",
        dataType : "json",
        data : {},
        success : function(data) {
            addNavigation(data.obj);
        }
    })
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    var trueName = create.trueName;
    $("#LoginTitle").html(trueName);
    $("#layui-nav-img").attr("src",create.avatarUrl);
    //获取个人信息
    var userId = create.userId;
    $.post("/Navigation/selUserInfo", {userId:userId}, function (res) {
        $(".dept_span").html(res.deptName);
        $(".login_time").html(res.loginDate);
        $(".login_city").html(res.loginLocation);
        $(".agin_time").html(res.loginAginDate);
        $(".login_ip").html(res.loginIp);
    });
    //拼接垂直导航栏
    function addNavigation(data) {
        var Navigation=""
        for(var i=0;i<data.length;i++){
            // Navigation+="<li class='layui-nav-item'>";
            Navigation+= "<li class='layui-nav-item'> <a data-id='"+ data.menu_id +"' ><i class='layui-icon "+data[i].icon+"' style='top: 3px;'> </i><cite>"+data[i].menu_name+"</cite> </a>"
            //  Navigation+="<a  href='javascript:;'  _href='#'> <i class='layui-icon "+data[i].icon+"'></i> <cite> "+data[i].menu_name+"</cite></a>";    //第一层
            if(data[i].children){
                for(var i1=0;i1<data[i].children.length;i1++){
                    Navigation+="<dl class='layui-nav-child'>";
                    Navigation+="<dd class=''>"
                    //   Navigation+="<a ><span onclick='fun1(\""+data[i].children[i1].url+"\",\""+data[i].children[i1].menu_name+"\")'>"+data[i].children[i1].menu_name+"</span></a>"         //第二级

                    if(data[i].children[i1].url==undefined){
                        Navigation+="<a  data-id='"+ data[i].children[i1].menu_id +"'><cite>"+data[i].children[i1].menu_name+"</cite></a>"
                    }else{
                        Navigation+="<a data-id='"+ data[i].children[i1].menu_id +"' href='javascript:;'  _href='"+data[i].children[i1].url+"'><cite>"+data[i].children[i1].menu_name+"</cite></a>"
                    }
                    if(data[i].children[i1].children) {
                        Navigation+= "<dl class='layui-nav-child'>"
                        for (var i2 = 0; i2 < data[i].children[i1].children.length; i2++) {
                            //  Navigation += "<dd><a onclick='fun1(\""+data[i].children[i1].children[i2].url+"\",\""+data[i].children[i1].children[i2].menu_name+"\")'>" + data[i].children[i1].children[i2].menu_name + "</a></dd>"        //第三级
                            Navigation += "<dd class=''><a data-id='"+  data[i].children[i1].children.menu_id +"' href='javascript:;'  _href='"+data[i].children[i1].children[i2].url+"'><cite>" + data[i].children[i1].children[i2].menu_name + "</cite></a></dd>"
                        }
                        Navigation+= "</dl>"
                    }
                    Navigation+= "</dd>"
                    Navigation+= "</dl>"
                }
            }
            Navigation+="</li>"
        }
        $(".layui-nav-tree").html(Navigation)
        layui.element.init()
    }
    //右上角退出
    $('.layui-icon-logout').on('click',function(){
        layer.confirm('是否退出该系统？', {
            btn: ['退出','取消'] //按钮
        }, function(){
            $.ajax({
                type:"post",
                url:"/Navigation/loginOut",
                data:{},
                dataType:"json",
                success:function (data) {
                    if(data){
                        //退出到登录页面
                        window.parent.location.href="/login/SyLogin.html";
                        layer.msg('退出成功', {icon: 1});
                    }
                }
            })
        }, function(){

        });
    })

    //导航的hover效果、二级菜单等功能，需要依赖element模块
    // larry-side-menu向左折叠
    if($(window).width()<750){
        trun = 0;
    }else{
        trun = 1;
    }
    $('.larry-side-menu').click(function() {
        if(trun){
            $('.x-side').animate({left: '0px'},200).siblings('.x-main').animate({left: '220px'},200);
            trun=0;
        }else{
            $('.x-side').animate({left: '-220px'},200).siblings('.x-main').animate({left: '0px'},200);
            trun=1;
        }
    });
    //监听导航点击
    element.on('nav(side)', function(elem){
        layui.element.init()
        title = elem.find('cite').text();
        url=$(this).attr('_href')
        id1=$(this).attr('data-id')
        if(url!=undefined) {
            createTab(id1, title, url);
        }
    });
    //监听菜单点击
    element.on('nav(sade)',function(elem){
        layui.element.init();
        title = elem.context.innerText;
        url=$(this).attr('src');
        id1=$(this).attr('_hrf');
        if(url!=undefined) {
            createTab(id1, title, url);
        }
    })
    //跳转账户设置
    $('#andim_a').bind('click' ,function (){
        title = '账户设置';
        url='/public/SetAccount.html';
        id1=3;
        if(url!=undefined) {
            createTab(id1, title, url);
        }
    })
    //创建选项卡
    function createTab(tabId, con , url) {
        var lis = [];
        $("#tabList #nav li").each(function () {
            lis.push($(this).attr("lay-id"));
        })
        if (lis.length <= 0 || lis.indexOf(tabId) == -1) {
            element.tabAdd('x-tab', {
                title: con,
                content: createIframe(url),
                id: tabId
            });
        }
        //切换选项卡（不需要写else）
        element.tabChange('x-tab', tabId);
    }
    //加载窗体
    function createIframe(url){
        return '<iframe src="'+url+'" style="width:100%;height:100%;" frameborder="0" ></iframe>'
    }
    //桌面首页展示
    var deptId = create.deptId;
    var val='';
    $.post("/personnel/selDeptList",val , function (res) {
        for(var a=0;a<res.obj.length;a++){
            if(res.obj[a].deptId==deptId){
                if(deptId==1){
                    $(".layui-show").html("<iframe  class=\"Tab1\" frameborder=\"0\" src=\"/personnel/PerHome.html\"></iframe>");
                    selPerNoticeNum();
                }else if(deptId==2){
                    $(".layui-show").html("<iframe  class=\"Tab1\" frameborder=\"0\" src=\"/teacher/TeaHome.html\"></iframe>");
                }else if(deptId==3){
                    $(".layui-show").html("<iframe  class=\"Tab1\" frameborder=\"0\" src=\"/operation/OpeHome.html\"></iframe>");
                    selOpeNoticeNum();
                }else if(deptId==4){
                    $(".layui-show").html("<iframe  class=\"Tab1\" frameborder=\"0\" src=\"/personnel/PerHome.html\"></iframe>");
                    selPerNoticeNum();
                }else{
                    $(".layui-show").html("<iframe  class=\"Tab1\" frameborder=\"0\" src=\"/personnel/PerHome.html\"></iframe>")
                    $("#layui-badge").html("0");
                }
            }
        }
    });
})
function showNoticeNum(){

}
//获取人事首页通知栏
function selPerNoticeNum(){
    //获取系统消息提示信息(人事)
    $.ajax({
        url:"/personnel/selUserNum",
        type:"get",
        dataType:"json",
        data:{},
        success:function(data) {
            selUserTab(data);
        }
    })
}
function selUserTab(data){
    if(data.applyWaitNum+data.feedbackWaitNum==0){
        $("#layui-badge").html("0");
    }else{
        $("#layui-badge").html(data.applyWaitNum+data.feedbackWaitNum)
    }
    if(data.applyWaitNum!=0){
        $("#notice_ul").append("<li class=\"notice_li\">\n" +
            "<span class=\"notice_span\">\n" +
            "<span>待审核的申请人员</span>\n" +
            "<span class=\"personnal_span\">（<span class=\"cloor_red\" _href='/personnal/html'>"+data.applyWaitNum+"</span>）</span>\n" +
            "</span>\n" +
            "</li>")
    }
    if(data.feedbackWaitNum!=0){
        $("#notice_ul").append("<li class=\"notice_li\">\n" +
            " <span class=\"notice_span \">\n" +
            "<span>待受理的用户反馈</span>\n" +
            "<span class=\"personnal_span\">（<span class=\"cloor_red\">"+data.feedbackWaitNum+"</span>）</span>\n" +
            "</span>\n" +
            "</li>")
    }

}
//获取运营首页通知栏
function selOpeNoticeNum(){
    $.post("/OpeHome/getAuditCourses",{"AuditCourses":3}, function (res) {
        $.post("/OpeHome/getAuditProduct",{"AuditProduct":2}, function (data) {
            setNoticeNum(res.obj[0].AuditCourses,data.obj[0].AuditProduct);
        });
    });
}
function setNoticeNum(AuditCourses,AuditProduct){
    if(AuditCourses+AuditProduct==0){
        $("#layui-badge").html("0");
    }else{
        $("#layui-badge").html(AuditCourses+AuditProduct)
    }
    if(AuditCourses!=0){
        $("#notice_ul").append("<li class=\"notice_li\">\n" +
            "<span class=\"notice_span\">\n" +
            "<span>待审核的课程信息</span>\n" +
            "<span class=\"personnal_span\">（<span class=\"cloor_red\" _href='/personnal/html'>"+AuditCourses+"</span>）</span>\n" +
            "</span>\n" +
            "</li>")
    }
    if(AuditProduct!=0){
        $("#notice_ul").append("<li class=\"notice_li\">\n" +
            " <span class=\"notice_span \">\n" +
            "<span>待审核的商品信息</span>\n" +
            "<span class=\"personnal_span\">（<span class=\"cloor_red\">"+AuditProduct+"</span>）</span>\n" +
            "</span>\n" +
            "</li>")
    }
}

//获取教师通知消息
function selTeaNoticeNum(){
    //待审核的课程
    //正在编译的课程
    //
    $.get('/teacher/getNotices',function(res){

    })
}